package com.example.myapplication4

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    private var name: String = ""
    private var gender: String = ""
    private var phone: String = ""
    private var highScore: String = ""

    private lateinit var nameTv: TextView
    private lateinit var genderTv: TextView
    private lateinit var phoneTv: TextView
    private lateinit var scoreTv: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        name = getString(R.string.default_name)
        gender = getString(R.string.default_gender)
        phone = getString(R.string.default_phone)
        highScore = getString(R.string.default_score)

        nameTv = findViewById(R.id.name_tv)
        genderTv = findViewById(R.id.gender_tv)
        phoneTv = findViewById(R.id.phone_tv)
        scoreTv = findViewById(R.id.score_tv)
        val editBtn = findViewById<Button>(R.id.edit_btn)
        val gameBtn = findViewById<Button>(R.id.game_btn)

        updateInfoDisplay()

        editBtn.setOnClickListener {
            val intent = Intent(this, EditInfoActivity::class.java).apply {
                putExtra("name", name)
                putExtra("gender", gender)
                putExtra("phone", phone)
            }
            startActivityForResult(intent, 1)
        }

        gameBtn.setOnClickListener {
            val intent = Intent(this, MainActivity3::class.java)
            startActivity(intent)
        }
    }

    private fun updateInfoDisplay() {
        nameTv.text = getString(R.string.name) + name
        genderTv.text = getString(R.string.gender) + gender
        phoneTv.text = getString(R.string.phone) + phone
        scoreTv.text = getString(R.string.high_score) + highScore
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 1 && resultCode == RESULT_OK) {
            data?.let {
                name = it.getStringExtra("name") ?: name
                gender = it.getStringExtra("gender") ?: gender
                phone = it.getStringExtra("phone") ?: phone
                updateInfoDisplay()
            }
        }
    }
}